# @author: uuz
# encoding = utf-8

from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, abort

from ext import db
from models import Hotel, ReservationNotice

notice_bp = Blueprint("notice", __name__, url_prefix='/api/notice')
api = Api(notice_bp)


class AddNoticeView(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)
        self.parser.add_argument('pet', type=int, required=True)
        self.parser.add_argument('outer_guest', type=int, required=True)
        self.parser.add_argument('checkin_time', type=str, required=True)
        self.parser.add_argument('checkout_time', type=str, required=True)
        self.parser.add_argument('special', type=str, required=False)

    def post(self):
        args = self.parser.parse_args()
        hotel_id = args.get('hotel_id')
        with_pet = args.get('pet')
        outer_guest = args.get('outer_guest')
        checkin_time = args.get('checkin_time')
        checkout_time = args.get('checkout_time')

        special = args.get('special')

        if Hotel.query.filter(Hotel.id == hotel_id).first():
            notice = ReservationNotice(hotel_id=hotel_id,
                                       with_pet=with_pet,
                                       outer_guest=outer_guest,
                                       check_in_time=checkin_time,
                                       check_out_time=checkout_time,
                                       special=special)
            db.session.add(notice)
            db.session.commit()
            return jsonify({"result": "OK"})
        else:
            abort(400, message="不存在该酒店id")


api.add_resource(AddNoticeView, '/Notice-Add/')


class QueryNoticeByHotelId(Resource):
    def __init__(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('hotel_id', type=int, required=True)

    def get(self):
        hotel_id = self.parser.parse_args().get('hotel_id')
        notice = ReservationNotice.query.filter(ReservationNotice.hotel_id == hotel_id).first()
        if notice:
            return {
                "id": notice.id,
                "hotel_id": notice.hotel_id,
                "with_pet": notice.with_pet,
                "outer_guest": notice.outer_guest,
                "check_in_time": notice.check_in_time,
                "check_out_time": notice.check_out_time,
                "special": notice.special
            }
        else:
            abort(400, message="酒店编号不存在")


api.add_resource(QueryNoticeByHotelId, '/QueryByHotelId/')